package avriServer.command;


public class Action {

}


//Service Actions
/*
ACTION_ALLOWED.put("say", new Action ("service"));
ACTION_ALLOWED.put("typing", new Action ("service"));
ACTION_ALLOWED.put("help", new Action ("service"));
ACTION_ALLOWED.put("disconnect", new Action ("service"));

ACTION_ALLOWED.put("keepAlive", new Action ("service", "<strong>/keepAlive : </strong>Keep Alive the comet connexion."));
ACTION_ALLOWED.put("login", new Action ("service", "<strong>/login :</strong> Log you on the chat. Need name and password (can be ='' for guest)."));
//Public Actions (user use it)
ACTION_ALLOWED.put("whois", new Action ("public", "<strong>/whois : </strong>whois on the current channel."));
ACTION_ALLOWED.put("join", new Action ("public", "<strong>/join Channel : </strong>join Channel, unsbuscribe the old one."));
ACTION_ALLOWED.put("subscribe", new Action ("public", "<strong>/subscribe Channel: </strong>join Channel."));
ACTION_ALLOWED.put("unsubscribe", new Action ("public", "<strong>/unsbuscribe Channel: </strong>unsbuscribe Channel."));
ACTION_ALLOWED.put("w", new Action ("public", "<strong>/w Name Message: </strong>wisp the Message to \"Name\""));
ACTION_ALLOWED.put("r", new Action ("public", "<strong>/r : answer a wisp </strong> ."));
//Admin Actions (only Admin can use it)
ACTION_ALLOWED.put("kick", new Action ("admin", "<strong>/kick Name : </strong> kick the user Name."));
ACTION_ALLOWED.put("clean", new Action ("admin", "<strong>/clean Channel : </strong> Clean the Channel."));
*/
/*
//message null
if (messageJson == null || message.equals(""))
	this.die(new JSONData().getBayeuxErrorHandShake("'message' parameter isn't define, or isn't correct."));
else {
	//we first get the name of the channel.
	String channel = messageJson.optString("channel");
//handShake channel : connection !
	if (channel.equals(Server.BAYEUX_CHANNEL_HANDSHAKE)) {
		//We just answer ok, and create a new client ID.
		this.die(new JSONData().getBayeuxSuccessHandShake(this.dispatcher.getUniqueClientId()));
	}
	else {
		/*at this step of the connection, we NEED :
		-> the clientId
		*/
		//clientId
		/*
String clientId = messageJson.optString("clientId");
		if(clientId.equals("")) 
			this.die(new JSONData().getBayeuxErrorConnection(clientId, "The clientId is undefined."));
//meta/Connect : connection asks -> we have to create the tunnel on this side.
		else if (channel.equals(Server.BAYEUX_CHANNEL_CONNECT)) {
			/*at this step of the connection, we NEED :
				-> the clientId (ok)
				-> the object ex which have to contains : 
					-> cometType //if == "" -> Webkit
					-> cometName //if == "" -> mooComet
					-> name //if == "" -> error
					-> pass //can ben == "" for guests
				so let's check if we have all this stuff.
			*//*
			JSONObject ext = null;
			try {
				ext = new JSONObject(messageJson.optString("ext"));
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			//cometType
			Integer cometType = null;
			try {
				cometType = Integer.parseInt(ext.optString("cometType"));
				if(!Server.COMET_TYPE.containsValue(cometType))
					cometType = Server.COMET_TYPE.get("Webkit") ;
			}
			catch (Exception e) {
				cometType = Server.COMET_TYPE.get("Webkit") ;
			}
			//cometName
			String cometName = ext.optString("cometName");
			if (cometName.equals(""))
				cometName = Server.DEFAULT_COMET_NAME ;
			//name
			String name = ext.optString("name");
			//pass
			String pass = ext.optString("pass");
			
			if(name.equals(""))
				this.die(new JSONData().getBayeuxErrorConnection(clientId, "The name is undefined."));
			else {	
				try {
					User u ;
					if(pass.equals("")) 
						u = new User(name);
					else
						u = DAOFactory.GET_DAO_USER().find(name, pass);
					u.setClientId(clientId);
					Waiter w = new Waiter(cometType, cometName, this.socket, this.br, this.pw, u) ;
					this.dispatcher.log(u, w);
					//we have to send some noise to trident to flush his buffer.
					if(Server.COMET_TYPE.get("Trident").equals(cometType))
						this.write("<span></span><span></span><span></span><span></span><span></span><span></span><span></span>");
					this.send(cometType);
					this.dispatcher.push(u, new JSONData().getBayeuxConnection(clientId));
				} 
				catch (IdentificationException e) {
					this.die(new JSONData().getBayeuxErrorConnection(clientId, e.toString()));
				}
				catch (ErrorConnectionException e) {
					this.die(new JSONData().getBayeuxErrorConnection(clientId, e.toString()));
				}
				catch (UserDisconnectedException e) {
					this.die(new JSONData().getBayeuxErrorConnection(clientId, e.toString()));
				}
			}
		}
///meta/disconnect -> disconnection from the client
		else if (channel.equals(Server.BAYEUX_CHANNEL_DISCONNECT)) {
			try {
				this.dispatcher.removeWaiter(new User().setClientId(clientId));
				this.die(new JSONData().getBayeuxDisconnection(clientId));
			}
			catch (ErrorDisconnectionException e){
				this.die(new JSONData().getBayeuxErrorDisconnection(clientId, e.toString()));
			}
			catch (UnexistantChannelException e){
				this.die(new JSONData().getBayeuxErrorDisconnection(clientId, e.toString()));
			}
		}		
///meta/subscribe	-> client asks for a subscribtion to a channel
		else if (channel.equals(Server.BAYEUX_CHANNEL_SUBSCRIBE)) {
			String subscribtion = messageJson.optString("subscription");
			if (subscribtion.equals(""))
				this.die(new JSONData().getBayeuxErrorSubscribtion(clientId, "value of subscription null"));
			else {
				try {
					User u = this.dispatcher.getUser(clientId);
					String mess = new JSONData().getInfo(this.dispatcher.getSavedMessage(subscribtion));
					this.dispatcher.push(u, new JSONData().getBayeuxPublish(Server.BAYEUX_CHANNEL_SERVICE+"oldMessage", mess));
					int numberUser = this.dispatcher.subscribe(subscribtion, new User().setClientId(clientId));	
					this.die(new JSONData().getBayeuxSubscribtion(clientId, subscribtion, numberUser));
				}
				catch (UserDisconnectedException e){
					this.die(new JSONData().getBayeuxErrorSubscribtion(clientId, e.toString()));
				}
				catch (UnexistantChannelException e){
					this.die(new JSONData().getBayeuxErrorSubscribtion(clientId, e.toString()));
				}
			}
		}
///meta/unsubscribe	-> client asks for an unsubscribtion to a channel
		else if (channel.equals(Server.BAYEUX_CHANNEL_UNSUBSCRIBE)) {
			String subscribtion = messageJson.optString("subscription");
			if (subscribtion.equals(""))
				this.die(new JSONData().getBayeuxErrorSubscribtion(clientId, "value of subscription null"));
			else {
				subscribtion = Input.SECURE(subscribtion);
				try {
					this.dispatcher.unsubscribe(subscribtion, new User().setClientId(clientId));
					this.die(new JSONData().getBayeuxUnsubscribtion(clientId));
				}
				catch (UserUnsuscribleException e){
					this.die(new JSONData().getBayeuxErrorUnsubscribtion(clientId, e.toString()));
				}
				catch (UnexistantChannelException e){
					this.die(new JSONData().getBayeuxErrorUnsubscribtion(clientId, e.toString()));
				}
			}
		}
///Service/ Channel : for exemple whois, keepAlive ect ect...
		else if(channel.length()>Server.BAYEUX_CHANNEL_SERVICE.length() && channel.substring(0, Server.BAYEUX_CHANNEL_SERVICE.length()).equals(Server.BAYEUX_CHANNEL_SERVICE)) {
			String service = channel.substring(Server.BAYEUX_CHANNEL_SERVICE.length(), channel.length());
			if (service.equals(""))
				this.die(new JSONData().getBayeuxErrorPublishResponse(channel, "service value is null"));
	//help
			else if(service.equals("help")) {
				try {
					User u = this.dispatcher.getUser(clientId);
					String mess = new JSONData().getHelp(Server.GET_LIST_ACTION(u.isAdmin())) ;
					this.dispatcher.push(u, new JSONData().getBayeuxPublish(channel, mess));							
					this.die(new JSONData().getBayeuxPublishResponse(channel));
				} catch (UserDisconnectedException e) {
					this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e.toString()));
				}
			}
	//whois
			else if(service.equals("whois")) {
				try {
					User u = this.dispatcher.getUser(clientId);
					String mess = new JSONData().getInfo(this.dispatcher.whois(clientId)) ;
					this.dispatcher.push(u, new JSONData().getBayeuxPublish(channel, mess));
					this.die(new JSONData().getBayeuxPublishResponse(channel));
				} catch (UserDisconnectedException e) {
					this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e.toString()));
				} 
			}
	//keepAlive
			else if(service.equals("keepAlive")) {
				try {
					User u =new User().setClientId(clientId) ;
					this.dispatcher.push(u, new JSONData().getKeepAlive());
					this.dispatcher.keepAlive(u);
					this.die(new JSONData().getBayeuxPublishResponse(channel));
				} catch (UserDisconnectedException e) {
					this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e.toString()));
				}
			}
			else
				this.die(new JSONData().getBayeuxErrorPublishResponse(channel, "unknown service"));
		}
//All others standard publish request.
		else {
		//Channel have to start by "/" 
			if (!channel.substring(0, 1).equals("/"))
				this.die(new JSONData().getBayeuxErrorPublishResponse(channel, "wrong format channel"));
			else {
				String data = messageJson.optString("data");
				if (data.equals(""))
					this.die(new JSONData().getBayeuxErrorPublishResponse(channel, "data value is empty"));
				else {
					JSONObject dataJson;
					String action ; 
					String value ; 
					try {
						dataJson = new JSONObject(data);
						action = dataJson.optString("action"); 
						value = dataJson.optString("value"); 
					} catch (JSONException e1) {
						dataJson = null;
						action = ""; 
						value = ""; 
					}
			//say
					if(action.equals("say") && !value.equals("")) {
						try {
							String name = this.dispatcher.getUser(clientId).getName();
							String mess = new JSONData().getMessage(name, Input.SECURE(value)) ;
							this.dispatcher.push(channel, new JSONData().getBayeuxPublish(channel, mess));
							//this.dispatcher.saveMessage(channel, mess);
							this.dispatcher.saveMessage(channel, Input.SECURE(value));
						} catch (UserDisconnectedException e1) {
							this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e1.toString()));
						} catch (UnexistantChannelException e) {
							this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e.toString()));
						}
						this.die(new JSONData().getBayeuxPublishResponse(channel));							
					}
			//typing
					else if(action.equals("typing") && !value.equals("")) {
						try {
							String name = this.dispatcher.getUser(clientId).getName();
							this.dispatcher.push(channel, new JSONData().getBayeuxPublish(channel, new JSONData().getTyping(name, value)));
							this.die(new JSONData().getBayeuxPublishResponse(channel));							
						} catch (UserDisconnectedException e1) {
							this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e1.toString()));
						} catch (UnexistantChannelException e) {
							this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e.toString()));
						}
					}
			//wips
					else if(action.equals("w")) {
						String nameWisp = dataJson.optString("nameWisp") ;
						String valueWisp = dataJson.optString("valueWisp") ;								

						if (nameWisp.equals("") || valueWisp.equals(""))
							this.die(new JSONData().getBayeuxErrorPublishResponse(channel, "name or value wips is empty"));
						else {
							try {
								
								nameWisp = Input.SECURE(nameWisp);
								valueWisp = Input.SECURE(valueWisp);
								
								Waiter w = this.dispatcher.getWaiter(nameWisp);
								if (w == null)
									throw new UserDisconnectedException ();
								User u = this.dispatcher.getUser(clientId);
								String mess = new JSONData().getWisp(u.getName(), Input.SECURE(valueWisp)) ;
								this.dispatcher.push(w.getUser(), new JSONData().getBayeuxPublish(channel, mess));
								this.dispatcher.push(u, new JSONData().getBayeuxPublish(channel, mess));
								this.die(new JSONData().getBayeuxPublishResponse(channel));
								
							} catch (UserDisconnectedException e) {
								this.die(new JSONData().getBayeuxErrorPublishResponse(channel, e.toString()));
							}
						}
					}
					else
						this.die(new JSONData().getBayeuxErrorPublishResponse(channel, "unknown request"));
				}
			}
		}
	}
}*/